﻿using System.IO;
using System.Windows.Ink;
using System.Windows.Markup;

namespace ThePresenter.Windows.Controls
{
	public static class StrokeCollectionExtensions
	{
		public static byte[] ToBytes(this StrokeCollection Strokes)
		{
			if (Strokes.Count == 0) return null;

			MemoryStream ms = new MemoryStream();
			XamlWriter.Save(Strokes, ms);

			byte[] buff = ms.ToArray();

			return buff;
		}

		public static StrokeCollection ToStrokes(this byte[] Bytes)
		{
			if (Bytes == null || Bytes.Length == 0)
				return new StrokeCollection();

			MemoryStream ms = new MemoryStream();
			ms.Write(Bytes, 0, Bytes.Length);
			ms.Position = 0;

			var o = XamlReader.Load(ms);

			if (o is StrokeCollection)
				return (StrokeCollection)o;
			else
				return null;
		}
	}
}
